package com.foreknow.dao.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Guestbook;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDAO implements GuestbookDao {
    @Override
    public boolean addMessage(Guestbook guestbook) throws SQLException {
        String sql = "insert into guestbook(name,phone,email,title,content,createdtime) values(?,?,?,?,?,?)";
        int x = jdbcTemplate.update(sql,guestbook.getName(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),guestbook.getContent(),guestbook.getCreatedtime());
        return x==1;
    }

    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql = "select * from guestbook";
        EntityMapping mapping = this.mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = this.jdbcTemplate.query(sql,mapping);
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql = "delete from guestbook where id=?";
        int x = this.jdbcTemplate.update(sql,id);
        return x==1;
    }

    @Override
    public boolean update(Guestbook gb) {
        String sql = "update guestbook set name=?, phone=?, email=?, title=?, content=?, createdtime=? where id=?";
        int x = this.jdbcTemplate.update(sql,gb.getName(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent(),gb.getCreatedtime(),gb.getId());
        return x==1;
    }

    @Override
    public Guestbook queryById(Integer id) {
        String sql = "select * from guestbook where id =?";
        EntityMapping mapping = this.mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,id);
        if (list.size()>0){
            Guestbook guestbook = (Guestbook) list.get(0);
            return guestbook;
        }
        return null;
    }
}
